/* enum */
import HtmlTagNameEnum from '@model/enum/HtmlTagNameEnum'
/* model */
import { 
  BaseSelectUserOptions, 
  BaseSelectUserMax, 
  BaseSelectUserPropsOptions,
  BaseSelectUserTypeIdEnum,
  SelectUserComponentUser,
  SelectUserComponentServiceProvider,
  SelectUserComponentRole,
  SelectUserComponentDepartment,
  SelectUser,
  BaseSelectUserModeEnum,
  BaseSelectUserBaseWindowDefaultOptions,
  BaseSelectUserTypeServiceProviderEnum
} from '@src/component/common/BaseSelectUser/model'
/* util */
import Log from '@src/util/log'
import { isArray, isNotFunction, isNotUndefined, isObject, isString, isUndefined } from '@src/util/type'
import { isSafeNumber } from 'pub-bbx-utils'
import { isOnlyCustomerServiceCloudVersion } from '@src/util/version'
/* types */
import { BaseSelectUserSelectedItemType, BaseSelectUserSelectedObjectType } from '@src/component/common/BaseSelectUser/types'
import i18n from '@src/locales'
/** 
 * @description 获取 vue 挂载元素
*/
function getMountElement(options: BaseSelectUserOptions): HTMLElement {
  
  // 暂不支持，后面看是否支持
  // if (isElement(options?.mountEl)) {
  //   return options?.mountEl as HTMLElement
  // }
  
  // if (isString(options?.mountEl)) {
  //   return document.getElementById(options?.mountEl as string) as HTMLElement
  // }
  
  const element = document.createElement(HtmlTagNameEnum.Div)
  
  appendBaseSelectUserMountElement(element)
  
  return element
}

function appendBaseSelectUserMountElement(element: HTMLElement): void {
  document.body.appendChild(element)
}

/** 
 * @description 获取选人组件配置项 max 最大值
*/
function getOptionsMax(options: BaseSelectUserOptions): number {
  try {
    
    let max = options?.max
    
    if (!max) {
      return BaseSelectUserMax
    }
    
    if (isString(max)) {
      max = parseInt(max as unknown as string)
      return max
    }
    
    if (isSafeNumber(max)) {
      return max as number
    }
    
    return BaseSelectUserMax
    
  } catch (error) {
    
    Log.error(error, getOptionsMax.name)
    
    return BaseSelectUserMax
  }
}

/** 
 * @description 获取选人组件配置 标题
*/
function getOptionsTitle(options: BaseSelectUserOptions): string {
  return options?.title || i18n.t('projectManage.setting.authSetting.selector') as string
}

/** 
 * @description 是否为多选状态
*/
function isMulti(options: BaseSelectUserPropsOptions): boolean {
  return options?.max != 1
}

/** 
 * @description 获取选人组件配置 选人组件是否绑定对应tab
 * -- 默认值为 false
*/
function getOptionsIsBind(options: BaseSelectUserOptions): boolean | undefined {
  return isUndefined(options?.isBind) ? false : options?.isBind
}

/** 
 * @description 是否严格的遵循父子不互相关联的做法
 * -- 默认值为 true
*/
function getOptionsIsCheckStrictly(options: BaseSelectUserOptions): boolean | undefined {
  return isUndefined(options?.isCheckStrictly) ? true : options?.isCheckStrictly
}

/** 
 * @description 获取选人组件配置 是否窗口唯一
 * -- 默认值为 undefined
*/
function getOptionsUnique(options: BaseSelectUserOptions): boolean | undefined {
  return isUndefined(options?.unique) ? undefined : options?.unique
}

/** 
 * @description 获取选人组件配置 是否可以选人
 * -- 默认值为 true
*/
function getOptionsIsCanChooseUser(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.isCanChooseUser) ? true : Boolean(options.isCanChooseUser)
}

/** 
 * @description 获取选人组件配置 是否可以选部门
 * -- 默认值为 false
*/
function getOptionsIsCanChooseDepartment(options: BaseSelectUserOptions): boolean {
  return Boolean(options?.isCanChooseDepartment)
}

/** 
 * @description 获取选人组件配置 是否可以选角色
 * -- 默认值为 false
*/
function getOptionsIsCanChooseRole(options: BaseSelectUserOptions): boolean {
  return Boolean(options?.isCanChooseRole)
}

/** 
 * @description 获取选人组件配置 是否可以选服务商
 * -- 默认值为 false
*/
function getOptionsIsCanChooseServiceProvider(options: BaseSelectUserOptions): boolean {
  return Boolean(options?.isCanChooseServiceProvider)
}

/** 
 * @description 获取选人组件配置 是否显示 服务商 tab 选项卡
 * -- 默认值为 true
*/
function getOptionsShowServiceProvider(options: BaseSelectUserOptions): boolean {
  
  // 仅客服云版本 不显示 服务商
  if (isOnlyCustomerServiceCloudVersion()) {
    return false
  }

  if (isNotUndefined(options?.showServiceProvider)) {
    return Boolean(options.showServiceProvider)
  }
  
  if (isUndefined(options?.typeServiceProvider)) {
    return true
  }
  
  if (String(options?.typeServiceProvider) == String(BaseSelectUserTypeServiceProviderEnum.ServiceProvider)) {
    return true
  }
  
  if (options?.typeServiceProvider === BaseSelectUserTypeServiceProviderEnum.All) {
    return true
  }
  
  return false
}

/** 
 * @description 获取选人组件配置 是否显示 角色 tab 选项卡
 * -- 默认值为 true
*/
function getOptionsShowRole(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.showRole) ? true : Boolean(options.showRole)
}

/**
 * @description 获取选人组件配置 是否显示 项目组tab 选项卡
 * -- 默认值为 false
 */
function getOptionsShowProjectTeam(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.showProjectTeam) ? false : Boolean(options.showProjectTeam)
}

/** 
 * @description 获取选人组件配置 是否显示 部门 tab 选项卡
 * -- 默认值为 true
*/
function getOptionsShowDepartment(options: BaseSelectUserOptions): boolean {
  
  if (isNotUndefined(options?.showDepartment)) {
    return Boolean(options.showDepartment)
  }
  
  if (isUndefined(options?.typeServiceProvider)) {
    return true
  }
  
  if (String(options?.typeServiceProvider) == String(BaseSelectUserTypeServiceProviderEnum.Department)) {
    return true
  }
  
  if (options?.typeServiceProvider === BaseSelectUserTypeServiceProviderEnum.All) {
    return true
  }
  
  return false
}

/** 
 * @description 获取选人组件配置 是否显示 已删除
 * -- 默认值为 true
*/
function getOptionsShowDelete(options: BaseSelectUserOptions): boolean {
  
  if (isNotUndefined(options?.showDelete)) {
    return Boolean(options.showDelete)
  }
  
  const mode = getOptionMode(options)
  
  return isAllMode(mode)
}

/** 
 * @description 获取选人组件配置 是否显示 未分配
 * -- 默认值为 true
*/
function getOptionsShowNoDistribute(options: BaseSelectUserOptions): boolean {
  
  if (isNotUndefined(options?.showNoDistribute)) {
    return Boolean(options.showNoDistribute)
  }
  
  const mode = getOptionMode(options)
  
  return isAllMode(mode)
}

/** 
 * @description 获取选人组件配置 是否显示 已注销
 * -- 默认值为 true
*/
function getOptionsShowCancel(options: BaseSelectUserOptions): boolean {
  
  if (isNotUndefined(options?.showCancel)) {
    return Boolean(options.showCancel)
  }
  
  const mode = getOptionMode(options)
  
  return isAllMode(mode)
}

/** 
 * @description 获取选人组件配置 是否显示 人员
 * -- 默认值为 true
*/
function getOptionsIsShowUser(options: BaseSelectUserOptions): boolean {
  
  if (isUndefined(options?.isShowUser)) {
    return true
  }
  
  return Boolean(options?.isShowUser)
}

/** 
 * @description 获取选人组件配置 是否显示 搜索
 * -- 默认值为 true
*/
function getOptionsIsShowSearch(options: BaseSelectUserOptions): boolean {
  
  if (isUndefined(options?.isShowSearch)) {
    return true
  }
  
  return Boolean(options?.isShowSearch)
}

/** 
 * @description 获取选人组件配置 是否显示 搜索部门人员列表
 * -- 默认值为 true
*/
function getOptionsIsShowSearchOrganizationUsers(options: BaseSelectUserOptions): boolean {  
  return isUndefined(options?.isShowSearchOrganizationUsers) ? true : Boolean(options?.isShowSearchOrganizationUsers)
}

/** 
 * @description 获取选人组件配置 是否显示 搜索服务商人员列表
 * -- 默认值为 true
*/
function getOptionsIsShowSearchServiceProvidersUsers(options: BaseSelectUserOptions): boolean {  
  return isUndefined(options?.isShowSearchServiceProviderUsers) ? true : Boolean(options?.isShowSearchServiceProviderUsers)
}

/** 
 * @description 获取选人组件配置 已选择人员列表
*/
function getOptionsSelectedUsers(options: BaseSelectUserOptions): SelectUser[] {
  
  // 是否只能选择一个用户
  const isOnlySelectOneUser = getOptionsMax(options) === 1 && getOptionsIsCanChooseUser(options)
  
  if (isArray(options?.selected)) {
    return isOnlySelectOneUser ? [] : options?.selected as SelectUserComponentUser[]
  }
  
  const selectedData: BaseSelectUserSelectedObjectType = options?.selected as BaseSelectUserSelectedObjectType
  if (isObject(options?.selected)) {
    return isOnlySelectOneUser ? [] : (selectedData?.users || [])
  }
  
  return []
}

/** 
 * @description 获取选人组件配置 已选择部门列表
*/
function getOptionsSelectedDepartments(options: BaseSelectUserOptions): SelectUserComponentDepartment[] {
  
  if (isArray(options?.selectedDepartments)) {
    return options.selectedDepartments as SelectUserComponentDepartment[]
  }
  
  const selectedData: BaseSelectUserSelectedObjectType = options?.selected as BaseSelectUserSelectedObjectType
  if (isObject(options?.selected)) {
    return (selectedData?.depts || []).concat(selectedData?.teams || [])
  }
  
  return []
}

/** 
 * @description 获取选人组件配置 已选择角色列表
*/
function getOptionsSelectedRoles(options: BaseSelectUserOptions): SelectUserComponentRole[] {
  
  if (isArray(options?.selectedRoles)) {
    return options.selectedRoles as SelectUserComponentRole[]
  }
  
  return []
}

/** 
 * @description 获取选人组件配置 已选择服务商列表
*/
function getOptionsSelectedServiceProviders(options: BaseSelectUserOptions): SelectUserComponentServiceProvider[] {
  
  if (isArray(options?.selectedServiceProviders)) {
    return options.selectedServiceProviders as SelectUserComponentServiceProvider[]
  }
  
  return []
}

/** 
 * @description 获取选人组件配置 已选择动态获取字段列表
*/
function getOptionsSelectedDynamicList(options: BaseSelectUserOptions): SelectUserComponentRole[] {
  
  // @ts-ignore
  if (isArray(options?.selectedDynamicList)) {
    // @ts-ignore
    return options.selectedDynamicList as SelectUserComponentRole[]
  }
  
  return []
}

/** 
 * @description 获取选人组件配置 已选择全部列表
*/
function getOptionsSelectedAll(options: BaseSelectUserOptions): BaseSelectUserSelectedItemType[] | null {
  
  if (isArray(options?.selectedAll)) {
    return options.selectedAll as SelectUserComponentServiceProvider[]
  }
  
  return null
}

/** 
 * @description 获取选人组件配置 已选择的数据
 * -- 默认值为 [] 空数组
*/
function getOptionsSelected(options: BaseSelectUserOptions): BaseSelectUserSelectedItemType[] {
  
  const selectedAll = getOptionsSelectedAll(options)
  
  if (selectedAll) {
    return selectedAll
  }
  
  const selected = getOptionsSelectedUsers(options)
  
  let selectedUsers = isArray(options?.selectedUsers) ? options.selectedUsers as SelectUserComponentUser[] : []
  
  selectedUsers = selectedUsers.concat(selected).map(item => {
    item.typeId = BaseSelectUserTypeIdEnum.User
    return item
  })
  
  const selectedDepartments = getOptionsSelectedDepartments(options).map(department => {
    !department.typeId && (department.typeId = BaseSelectUserTypeIdEnum.Department);
    return department
  })
  
  const selectedRoles = getOptionsSelectedRoles(options).map(role => {
    role.typeId = BaseSelectUserTypeIdEnum.Role
    return role
  })
  
  const selectedProviders = getOptionsSelectedServiceProviders(options).map(serviceProvider => {
    serviceProvider.typeId = BaseSelectUserTypeIdEnum.ServiceProvider
    return serviceProvider
  })
  
  const selectedDynamicList = getOptionsSelectedDynamicList(options).map(dynamic => {
    dynamic.typeId = BaseSelectUserTypeIdEnum.Dynamic
    return dynamic
  })
  
  return [...selectedUsers, ...selectedDepartments, ...selectedRoles, ...selectedProviders, ...selectedDynamicList]
}

/** 
 * @description 获取选人配置模式
*/
function getOptionMode(options: BaseSelectUserOptions): BaseSelectUserModeEnum {
  return options?.mode || BaseSelectUserModeEnum.All
}

/** 
 * @description 获取选人组件 全部模式 
 * -- 包含已删除，待分配，已注销数据。
*/
function isAllMode(mode: BaseSelectUserModeEnum) {
  return mode == BaseSelectUserModeEnum.All
}

/** 
 * @description 获取选人组件 获取部门列表方法
*/
function getOptionsFetchDepartmentList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchDepartmentList)) {
    return undefined
  }
  
  return options.fetchDepartmentList
}

/** 
 * @description 获取选人组件 获取部门人员列表方法
*/
function getOptionsFetchDepartmentUserList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchDepartmentUserList)) {
    return undefined
  }
  
  return options.fetchDepartmentUserList
}

/** 
 * @description 获取选人组件 获取搜索人员列表方法
*/
function getOptionsFetchSearchUserList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchSearchUserList)) {
    return undefined
  }
  
  return options.fetchSearchUserList
}

/** 
 * @description 获取选人组件 获取项目列表方法
*/
function getOptionsFetchProjectTeamList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchProjectTeamList)) {
    return undefined
  }
  
  return options.fetchProjectTeamList
}

/** 
 * @description 获取选人组件 获取项目用户列表方法
*/
function getOptionsFetchProjectTeamUserList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchProjectTeamUserList)) {
    return undefined
  }
  
  return options.fetchProjectTeamUserList
}

/** 
 * @description 获取选人组件 获取搜索更多人员列表方法
*/
function getOptionsFetchSearchMoreUserList(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.fetchSearchMoreUserList)) {
    return undefined
  }
  
  return options.fetchSearchMoreUserList
}

/** 
 * @description 获取选人组件 窗口配置项
*/
function getOptionsBaseWindowOptions(options: BaseSelectUserOptions) {
  
  if (isUndefined(options?.baseWindowOptions)) {
    return BaseSelectUserBaseWindowDefaultOptions
  }
  
  return {
    ...BaseSelectUserBaseWindowDefaultOptions,
    ...(options.baseWindowOptions || {})
  }
}

/** 
 * @description 获取选人组件配置 是否为 团队模式。如果传了参数，就以前端的参数为准，如果没传，就以租户的配置为主。
 * -- 默认值为 undefined
*/
function getOptionsIsTag(options: BaseSelectUserOptions): boolean | undefined {
  return options?.isTag
}

/** 
 * @description 区分全部权限和团队权限 目前只用在事件
 * -- 默认值为 undefined
*/
function getOptionsIsAuthKey(options: BaseSelectUserOptions): string | undefined {
  return options?.authKey
}

/** 
 * @description 获取选人组件配置 是否窗口置顶。
 * -- 默认值为 true
*/
function getOptionsTop(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.top) ? true : Boolean(options.top)
}

/** 
 * @description 获取选人组件配置 是否需要验证邮箱
 * -- 默认值为 false
*/
function getOptionsIsNeedEmail(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.isNeedEmail) ? false : Boolean(options.isNeedEmail)
}

/** 
 * @description 获取选人组件配置 是否显示 动态获取 tab
 * -- 默认值为 false
*/
function getOptionsIsShowDynamic(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.isShowDynamic) ? false : Boolean(options.isShowDynamic)
}

/** 
 * @description 获取选人组件配置 是否显示 动态获取 tab
 * -- 默认值为 false
*/
function getOptionsCurrentTabType(options: BaseSelectUserOptions) {
  return options?.currentTabType || ''
}


/** 
 * @description 获取选人组件 自定义显示动态获取 tab 的内容
*/
function getOptionsRenderDynamicContent(options: BaseSelectUserOptions) {
  return options?.renderDynamicContent
}

/**
 * @description 获取选人组件 自定义显示动态获取 tab 的内容
*/
function getOptionsRenderDynamicName(options: BaseSelectUserOptions) {
  return options?.DynamicTabName
}

/**
 * @description 获取选人组件 筛选是否展示
*/
function getOptionsRenderIsScreenSelect(options: BaseSelectUserOptions) {
  return options?.isScreenSelect
}

/**
 * @description 获取选人组件 筛选使用自定义模板
*/
function getOptionsRenderIsRenderFilterContent(options: BaseSelectUserOptions) {
  return options?.isRenderFilterContent
}

/**
 * @description 获取选人组件 筛选使用自定义模板数据
*/
function getOptionsRenderRenderFilterContent(options: BaseSelectUserOptions) {
  return options?.renderFilterContent
}

/** 
 * @description 获取选人组件配置 是否异步确认
*/
function getOptionsAsyncConfirm(options: BaseSelectUserOptions) {
  return options?.asyncConfirm
}

/**
 * @description 获取选人组件配置 服务商成员是否显示部门 tab
 * -- 默认值为 false
*/
function getOptionsShowTagIfSpUser(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.showTagIfSpUser) ? false : Boolean(options.showTagIfSpUser)
}

/**
 * @description 获取选人组件配置 服务商是否显示全部
 * -- 默认值为 false
*/
function getOptionsShowSpAll(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.showSpAll) ? false : Boolean(options.showSpAll)
}

/**
 * @description 获取选人组件配置 是否为触发器消息 选择人员
 * -- 默认值为 false
*/
function getOptionsIsTrigger(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.isTrigger) ? false : Boolean(options.isTrigger)
}

/**
 * @description 获取选人组件配置 是否只显示 服务云 角色
 * -- 默认值为 false
*/
function getOptionsIsOnlyShowBusinessServiceCloudRole(options: BaseSelectUserOptions): boolean {
  return isUndefined(options?.isOnlyShowBusinessServiceCloudRole) ? false : Boolean(options.isOnlyShowBusinessServiceCloudRole)
}


/**
 * @description服务商是否过滤掉没有工程师的
 * -- 默认值为 false
 */
function getOptionsIsHideNullProvider(options: BaseSelectUserOptions): boolean | undefined {
  return isUndefined(options?.hideNullProvider) ? false : Boolean(options.hideNullProvider)
}
	
/** 
 * @description 获取选人组件 实例
*/
function getOptionsGetInstance(options: BaseSelectUserOptions) {
  
  if (isNotFunction(options?.getInstance)) {
    return () => {}
  }
  
  return options.getInstance as Function
}
export {
  getOptionsMax,
  getMountElement,
  getOptionsTitle,
  isMulti,
  getOptionsIsBind,
  getOptionsIsCheckStrictly,
  getOptionsUnique,
  appendBaseSelectUserMountElement,
  getOptionsSelected,
  getOptionsIsCanChooseUser,
  getOptionsIsCanChooseDepartment,
  getOptionsIsCanChooseRole,
  getOptionsIsCanChooseServiceProvider,
  getOptionsShowServiceProvider,
  getOptionsShowRole,
  getOptionsShowProjectTeam,
  getOptionsShowDelete,
  getOptionsShowNoDistribute,
  getOptionsShowCancel,
  getOptionsIsShowUser,
  getOptionsIsShowSearch,
  getOptionsFetchDepartmentList,
  getOptionsFetchDepartmentUserList,
  getOptionsFetchProjectTeamList,
  getOptionsFetchProjectTeamUserList,
  getOptionsBaseWindowOptions,
  getOptionsIsTag,
  getOptionsIsAuthKey,
  getOptionsShowDepartment,
  getOptionsFetchSearchUserList,
  getOptionsFetchSearchMoreUserList,
  getOptionsIsShowSearchOrganizationUsers,
  getOptionsIsShowSearchServiceProvidersUsers,
  getOptionsTop,
  getOptionsIsNeedEmail,
  getOptionsIsShowDynamic,
  getOptionsCurrentTabType,
  getOptionsRenderDynamicContent,
  getOptionsShowTagIfSpUser,
  getOptionsShowSpAll,
  getOptionsRenderDynamicName,
  getOptionsRenderIsScreenSelect,
  getOptionsIsTrigger,
  getOptionsRenderIsRenderFilterContent,
  getOptionsRenderRenderFilterContent,
  getOptionsIsOnlyShowBusinessServiceCloudRole,
  getOptionsIsHideNullProvider,
  getOptionsGetInstance,
  getOptionsAsyncConfirm
}
